﻿// Decompiled by AS3 Sorcerer 6.78
// www.buraks.com/as3sorcerer

//com.wdcgame.engine.nape.utils.TerrainBody

package com.game.engine.nape.utils
{
    import nape.geom.IsoFunction;
    import nape.geom.Vec2;
    import nape.phys.Body;
    import flash.display.BitmapData;
    import nape.geom.AABB;
    import nape.geom.MarchingSquares;
    import nape.geom.GeomPolyList;
    import nape.space.Space;
    import nape.phys.BodyType;
    import nape.shape.Polygon;
    import nape.geom.GeomPoly;

    public class TerrainBody implements IsoFunction 
    {

        private var _cellSize:Vec2;
        private var _body:Body;
        private var bd:BitmapData;

        public function TerrainBody(_arg_1:BitmapData, _arg_2:Number, _arg_3:Space, _arg_4:int, _arg_5:int, _arg_6:BodyType, _arg_7:int=1)
        {
            this.bd = _arg_1;
            _cellSize = new Vec2(_arg_2, _arg_2);
            _body = new Body(_arg_6, new Vec2(_arg_4, _arg_5));
            var _local_8:AABB = new AABB(0, 0, _arg_1.width, _arg_1.height);
            var _local_9:GeomPolyList = MarchingSquares.run(this, _local_8, _cellSize, _arg_7, null, true, null);
            _local_9.foreach(af);
            _body.align();
            _body.space = _arg_3;
        }

        public function get body():Body
        {
            return (_body);
        }

        private function af(_arg_1:GeomPoly):void
        {
            var g = _arg_1;
            if (g)
            {
                var qolys:GeomPolyList = g.simplify(1).convexDecomposition(true);
                qolys.foreach(function (_arg_1:GeomPoly):void
                {
                    var _local_2:Polygon = new Polygon(_arg_1);
                    _local_2.filter.collisionGroup = CollisionGroup.FLOOR;
                    _body.shapes.push(_local_2);
                });
            }
        }

        public function iso(_arg_1:Number, _arg_2:Number):Number
        {
            if (bd.getPixel32(_arg_1, _arg_2) > 0)
            {
                return (-1);
            }
            return (1);
        }


    }
}//package com.wdcgame.engine.nape.utils

